package com.example.esaclient;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class NewAccountActivity extends Activity {
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_account);
		
	}

	
	public void registerCompany(View v){
		Boolean NotValid =false;
		String errString="";
		final String name,email, pwd1,pwd2,desc;
		EditText txtName =(EditText)findViewById(R.id.txtName);
		EditText txtEmail =(EditText)findViewById(R.id.txtEmail);
		EditText txtPwd1 =(EditText)findViewById(R.id.txtPwd1);
		EditText txtPwd2 =(EditText)findViewById(R.id.txtPwd2);
		EditText txtDesc =(EditText)findViewById(R.id.txtDesc);	
		
		name=txtName.getText().toString();
		email=txtEmail.getText().toString();
		pwd1=txtPwd1.getText().toString();
		pwd2=txtPwd2.getText().toString();
		desc=txtDesc.getText().toString();
		
		
		if(name.trim().equals("") ||email.trim().equals("") ||pwd1.trim().equals("") || pwd2.trim().equals("")|| desc.trim().equals(""))
		{
			NotValid =true;
			errString ="All fields are required! ";
		}
		if(!(pwd1.equals(pwd2))) {
			
			NotValid =true;
			errString +="Passwords do not match! ";			
		}
		
		if(NotValid){
			
	 		    	TextView tvAccountCreated;
	 		    	tvAccountCreated= (TextView)findViewById(R.id.txtAccountCreated);
	 		    	tvAccountCreated.setText(errString);
	 		    	tvAccountCreated.setTextColor(Color.RED);
	 		    	tvAccountCreated.setVisibility(View.VISIBLE);      
	 		   
		}else{
		
			// calling webService
			new RegisterCompany(this).execute(email,pwd1,name,desc);
			
	 
        	     
		}
		
		
		
	}
	
	
	public void esaResult(Boolean res){
		TextView tvAccountCreated;
    	tvAccountCreated= (TextView)findViewById(R.id.txtAccountCreated);
		if(res){
			tvAccountCreated.setText("New Account successfully created!");
		    	tvAccountCreated.setTextColor(Color.GREEN);
		    	tvAccountCreated.setVisibility(View.VISIBLE);   
		    	//clear all fields  -- not necessary now
		    	/*ViewGroup group = (ViewGroup)findViewById(R.id.vgNewAccount);
		    	for (int i = 0, count = group.getChildCount(); i < count; ++i) {
		    	    View view = group.getChildAt(i);
		    	    if (view instanceof EditText) {
		    	        ((EditText)view).setText("");
		    	    }
		    	 }*/
		    	
		    	Toast.makeText(this, "New Account successfully created!", Toast.LENGTH_SHORT).show();	
		    	finish();
		}else{
			
		    	tvAccountCreated.setText("Error creating new account.");
		    	tvAccountCreated.setTextColor(Color.RED);
		    	tvAccountCreated.setVisibility(View.VISIBLE); 
		}
		
	}

}
